package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type DeviceObjInfEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名

	ObjectName string // 通道类型
	ValueType  string // 通道名称
}

type deviceObjInfPo struct {
	Id           int64  `json:"id"`           // 对象id
	CreateTime   int64  `json:"createTime"`   // 创建时间
	UpdateTime   int64  `json:"updateTime"`   // 更新时间
	Manufacturer string `json:"manufacturer"` // 设备厂商名称
	DeviceType   string `json:"deviceType"`   // 设备类型名
	ObjectName   string `json:"objectName"`   // 通道类型
	ValueType    string `json:"valueType"`    // 通道名称
}

func (e *DeviceObjInfEntity) Type() string {
	return "DeviceObjInfEntity"
}

func (e *DeviceObjInfEntity) TableName() string {
	return ""
}

func (e *DeviceObjInfEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *DeviceObjInfEntity) Instance() *DeviceObjInfEntity {
	return &DeviceObjInfEntity{
		BaseEntity: new(commEntity.BaseEntity),
	}
}

func (e *DeviceObjInfEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *DeviceObjInfEntity) NewPo() interface{} {
	return &deviceObjInfPo{}
}

func (e *DeviceObjInfEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *DeviceObjInfEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.Manufacturer)
	p = append(p, e.DeviceType)
	p = append(p, e.ObjectName)
	return p
}

func (e *DeviceObjInfEntity) BuildByJson(jsn *string) error {
	var po deviceObjInfPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceObjInfEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceObjInfEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*deviceObjInfPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.ObjectName = po.ObjectName
	ptr.ValueType = po.ValueType
}

func (e *DeviceObjInfEntity) BuildToPo() interface{} {
	ptr := e
	po := deviceObjInfPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.ObjectName = ptr.ObjectName
	po.ValueType = ptr.ValueType

	return &po
}
